package com.am.user.controller;

import com.am.user.domain.Result;
import com.am.user.domain.dto.LoginFormDTO;
import com.am.user.domain.dto.UserDTO;
import com.am.user.domain.po.User;
import com.am.user.domain.vo.UserLoginVO;
import com.am.user.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {
    private final IUserService userService;

    // 登录验证
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody @Validated LoginFormDTO loginFormDTO){
        return userService.login(loginFormDTO);
    }

    // 发送短信
    @PostMapping("/sendMsg")
    public Result<String> sendMsg(@RequestParam("phone") String phone){
        return userService.sendMsg(phone);
    }

    // 个人信息
    @GetMapping("/getuser/{id}")
    public Result getMyInfo(@PathVariable int id){
        User user = userService.getById(id);
        return Result.success(user);
    }

    // 修改个人信息
    @GetMapping("/updateMyInfo")
    public Result updateMyInfo(@RequestBody UserDTO userDTO){
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        boolean update = userService.updateById(user);
        if (update) {
            return Result.success();
        }else {
            return Result.error("更新失败");
        }
    }


}
